/* @(#)GSManufactureAddViewAction.java    V0.1
 *
 * Project: GSdevelop
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   owen         2006-3-1
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.action;

import java.util.ArrayList;
import java.util.HashMap;

import com.hitrust.gs.bean.GSManufactureBean;
import com.hitrust.gs.bean.GSManufactureCategoryBean;
import com.hitrust.gs.bean.GSProviderBean;
import com.hitrust.gs.business.GSManufactureCategoryProcessor;
import com.hitrust.gs.business.GSManufactureProcessor;
import com.hitrust.gs.business.GSProviderProcessor;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.service.GSBaseManager;

/**
 * 
 */
public class GSManufactureModifyInitAction extends GSBaseAction {

    public String execute(GSBaseForm aForm) throws Exception {
        GSManufactureProcessor tGSManufProcessor = new GSManufactureProcessor();
        GSManufactureCategoryProcessor mcp = new GSManufactureCategoryProcessor();
        GSProviderProcessor pp = new GSProviderProcessor();
        try{
            //取得所有供应商
            ArrayList tProvider = GSBaseManager.getAllProvider();
            aForm.getRequest().setAttribute("Provider",tProvider);
            //取得产品等级为1级的产品种类
            ArrayList tManuf1 = tGSManufProcessor.getManufactureType("1");
            aForm.getRequest().setAttribute("manufacture1",tManuf1);
            //取得产品等级为2级的产品种类
            ArrayList tManuf2 = tGSManufProcessor.getManufactureType("2");
//            aForm.getRequest().setAttribute("manufacture2",tList);
            //取得产品等级为3级的产品种类
            ArrayList tManuf3 = tGSManufProcessor.getManufactureType("3");
//            aForm.getRequest().setAttribute("manufacture3",tList);
            //将每一级产品种类下一级产品种类取出组成ArrayList并放入HASHMAP中建立对应关系
            HashMap tMap1 = new HashMap();
            HashMap tMap2 = new HashMap();
            if(tManuf1 != null ){
                for(int i=0; i<tManuf1.size(); i++){
                    //一级产品种类
                    GSManufactureCategoryBean tGSManufCategyBean = (GSManufactureCategoryBean)tManuf1.get(i);
                    if(tManuf2 != null){
                        ArrayList tList1 = new ArrayList();
                        for(int j=0; j<tManuf2.size();j++){
                            //二级产品种类
                            GSManufactureCategoryBean tGSManufactureBean1 = (GSManufactureCategoryBean)tManuf2.get(j);
                            if(tGSManufCategyBean.getGs_manufacture_category_id().equals(tGSManufactureBean1.getGs_upper_manufacture_category())){
                                tList1.add(tGSManufactureBean1);
                            }
                            ArrayList tList2 = new ArrayList();
                            for(int k=0; k<tManuf3.size(); k++){
                                //三级产品种类
                               GSManufactureCategoryBean tGSManufactureBean2 = (GSManufactureCategoryBean)tManuf3.get(k);
                               if(tGSManufactureBean1.getGs_manufacture_category_id().equals(tGSManufactureBean2.getGs_upper_manufacture_category())){
                                  tList2.add(tGSManufactureBean2);
                               }
                            }
                            if(tList2.size() > 0)
                                tMap2.put(""+tGSManufactureBean1.getGs_manufacture_category_id(),tList2);
                        }
                        if(tList1.size() > 0)
                            tMap1.put(""+tGSManufCategyBean.getGs_manufacture_category_id(),tList1);
                    }
                }
            }
            aForm.getRequest().setAttribute("manufacturetyp2",tMap1);
            aForm.getRequest().setAttribute("manufacturetyp3",tMap2);
            
            
            String manufactureId = aForm.getRequest().getParameter("manufactureid");
            GSManufactureBean bean = tGSManufProcessor.getManufactureDetail(manufactureId);
//          查询对应产品种类的上级级别
            GSManufactureCategoryBean cBean = mcp.getCategory(bean.getGs_manufacture_category_id());
            String[] categoryStr = new String[]{"","",""};
            
            if(bean.getGs_manufacture_id() != null && cBean != null){
                int level = Integer.parseInt(cBean.getGs_manufacture_level());
                categoryStr[level-1] = cBean.getGs_manufacture_category_id()==null?"":cBean.getGs_manufacture_category_id();
                while(level != 1){
                    GSManufactureCategoryBean parentBean = mcp.getCategory(cBean.getGs_upper_manufacture_category());
                    categoryStr[level-2] = parentBean.getGs_manufacture_category_id()==null?"":parentBean.getGs_manufacture_category_id();
                    cBean = parentBean; 
                    level--;
                }
                
            }
            //查询供应商名称
            GSProviderBean pBean = pp.getProviderDetail(bean.getGs_provider_id());
            String providerName = pBean.getGs_provider_name();
            //增加配置文件 判断湖南巴士计划价
            if(GSBaseConstants.PLANPRICE.equals("true")){
                aForm.getRequest().setAttribute("isPlanprice", "1");
            }else{
                aForm.getRequest().setAttribute("isPlanprice", "0");
            }
            
            aForm.getRequest().setAttribute("categorystr",categoryStr);
            aForm.getRequest().setAttribute("providername",providerName);
            aForm.getRequest().setAttribute("manufacturebean",bean);
        }catch(GSException e){
            throw e;
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException("进入产品信息修改页面发生异常");
        }
        return GSBaseConstants.FORWARD_SUCCESS;
    }

}
